package com.baizhi.controller;


import com.baizhi.entity.Admin;
import com.baizhi.entity.Category;
import com.baizhi.service.ICategoryService;
import com.baizhi.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品三级分类 前端控制器
 * </p>
 *
 * @author longyang
 * @since 2022-01-19
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private ICategoryService iCategoryService;
    //根据父类别的编号查询该类别下面所有的子类别
    @GetMapping("selectChildrenCategory/{categoryId}")
    public ResultEntity<List<Category>> selectChildrenCategory(@PathVariable("categoryId")Integer categoryId){
        List<Category> categories = iCategoryService.selectCategorySon(categoryId);
        if (categories!=null){
            //成功
            return ResultEntity.ok(categories);
        }else {
            //失败
            return  ResultEntity.error();
        }
    }
    //添加类别
    @PostMapping("add")
    public ResultEntity<String> addCategory(Category category){
        Integer integer = iCategoryService.addCategory(category);
        if (integer==1){
            //成功
            return ResultEntity.ok();
        }else {
            //失败
            return  ResultEntity.error();
        }
    }
    //删除类别
    @DeleteMapping("delete/{categoryId}")
    public ResultEntity<String> deleteCategory(@PathVariable("categoryId") Integer categoryId){
        Integer integer = iCategoryService.deleteCategory(categoryId);
        if (integer==1){
            //成功
            return ResultEntity.ok();
        }else {
            //失败
            return  ResultEntity.error();
        }
    }
    //根据id查询类别信息
    @GetMapping("selectOne/{categoryId}")
    public ResultEntity<Category> selectOneCategory(@PathVariable("categoryId") Integer categoryId){
        Category category = iCategoryService.selectCategoryById(categoryId);
        if (category!=null){
            //成功
            return ResultEntity.ok(category);
        }else {
            //失败
            return  ResultEntity.error();
        }
    }
    //修改类别
    @PutMapping("update")
    public ResultEntity<String> updateCategory(Category category){
        Integer integer = iCategoryService.updateCategory(category);
        if (integer==1){
            //成功
            return ResultEntity.ok();
        }else {
            //失败
            return  ResultEntity.error();
        }
    }
    //查询整个类别树
    @GetMapping("selectTree")
    public ResultEntity<List<Category>> selectTreeCategory(){
        //查所有类别数据
        List<Category> categories = iCategoryService.selectCategoryTree();
        //过滤一级类别
        List<Category> collect = categories.stream().filter(item ->{
            return item.getParentId() == 0;
            //重新映射夫类别拥有的子类别 递归实现
        }).map(map -> {
            map.setCates(getSons(categories, map));
            return map;
        }).collect(Collectors.toList());

        if (categories!=null){
            //成功
            return ResultEntity.ok(collect);
        }else {
            //失败
            return  ResultEntity.error();
        }
    }
    private List<Category> getSons(List<Category> categories, Category map) {
        List<Category> collect = categories.stream().filter(item -> {
            //过滤出子类别
            return item.getParentId() == map.getCategoryId();
        }).map(map2 -> {
            map2.setCates(getSons(categories, map2));
            //将子类别赋值给夫类别
            return map2;
        }).collect(Collectors.toList());
        return collect;
    }
}

